package com.simpleaccount.Home;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.simpleaccount.Common.Utils;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2017/7/11.
 */
//
//public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder>{

//
//    public MyRecyclerViewAdapter(List<Entry> entries,Context context){
//        this.entries=entries;
//        this.context=context;
//        newEntries=setDates();
//    }
//

//

//
//    @Override
//    public int getItemCount() {
//        return newEntries.size();
//    }


//}

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder> {

    static final String COLOR_GREY="#808080";
    static final String COLOR_BLACK="#000000";
    private List<Entry> entries;
    private List<Entry> newEntries;
    Context context;

    public MyRecyclerViewAdapter(List<Entry> entries,Context context){
        this.context=context;
        this.entries=entries;
        newEntries=setDates();
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.base_rv_entry,parent,false);
        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        Entry entry = newEntries.get(position);

        TextView tvIncome = holder.getTvIncome();
        TextView tvOutcome = holder.getTvOutcome();

        tvIncome.setTextColor(Color.parseColor(COLOR_BLACK));
        tvOutcome.setTextColor(Color.parseColor(COLOR_BLACK));
        //设置时间线上的图片
        Utils.setImage(holder.getIvCategory(),entry.getCategory());
        int type =entry.getType();
        if(0==type){
            tvOutcome.setText("   "+entry.getCategory()+" "+String.format("%.2f",entry.getMoney()));
            tvIncome.setText("");
        }else if(1==type) {
            tvIncome.setText(String.format("%.2f",entry.getMoney())+" "+entry.getCategory()+"   ");
            tvOutcome.setText("");
        }else {
            tvOutcome.setTextColor(Color.parseColor(COLOR_GREY));
            tvIncome.setTextColor(Color.parseColor(COLOR_GREY));
            tvOutcome.setText("   "+String.format("%.2f",entry.getMoney()));
            if(-1==type){
                SimpleDateFormat sdf2 = new SimpleDateFormat("d日",Locale.CHINA);
                tvIncome.setText(sdf2.format(entry.getDate()));
            }else if(-2==type){
                SimpleDateFormat sdf2 = new SimpleDateFormat("M月d日",Locale.CHINA);
                tvIncome.setText(sdf2.format(entry.getDate()));
            }else if(-3==type){
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年M月d日",Locale.CHINA);
                tvIncome.setText(sdf2.format(entry.getDate()));
            }else if(-6==type){
                SimpleDateFormat sdf2 = new SimpleDateFormat("今天",Locale.CHINA);
                tvIncome.setText(sdf2.format(entry.getDate()));
            }
        }
    }

    @Override
    public int getItemCount() {
        return newEntries.size();
    }


    public int isFirstDay(int pos){
        if(pos==0){
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
            String curTime = sdf.format(System.currentTimeMillis());
            String eTime =sdf.format(entries.get(0).getDate().getTime());
            if(curTime.equals(eTime)){
                return 6;
            }
            return 1;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(entries.get(pos).getDate());
        Calendar calendar1 =Calendar.getInstance();
        calendar1.setTime(entries.get(pos-1).getDate());
        if (calendar.get(Calendar.YEAR)<calendar1.get(Calendar.YEAR)){
            return 3;
        }else if(calendar.get(Calendar.MONTH)<calendar1.get(Calendar.MONTH)){
            return 2;
        }else if(calendar.get(Calendar.DATE)<calendar1.get(Calendar.DATE)){
            return 1;
        }else {
            return 0;
        }
    }
    public List<Entry> setDates(){
        List<Entry> newEntries = new ArrayList<>();
        for(int i=0;i<entries.size();i++){
            Entry entry=entries.get(i);
            int res=isFirstDay(i);
            if(3==res){
                newEntries.add(new Entry(-3,entry.getDate(),getMoneyOfDay(i)));
                newEntries.add(entry);
            }else if(2==res){
                newEntries.add(new Entry(-2,entry.getDate(),getMoneyOfDay(i)));
                newEntries.add(entry);
            }else if(1==res){
                newEntries.add(new Entry(-1,entry.getDate(),getMoneyOfDay(i)));
                newEntries.add(entry);
            }else if(6==res){
                newEntries.add(new Entry(-6,entry.getDate(),getMoneyOfDay(i)));
                newEntries.add(entry);
            }
            else {
                newEntries.add(entries.get(i));
            }
        }
        return newEntries;
    }
    public void notifyData(List<Entry> list) {
        entries=list;
        newEntries=setDates();
            notifyDataSetChanged();
    }
    public double getMoneyOfDay(int begin){
        double sum=entries.get(begin).getMoney();
        int end=begin+1;
        for(;end<entries.size();end++){
            if(0!=isFirstDay(end)){
                break;
            }else {
                sum+=entries.get(end).getMoney();
            }
        }
        return sum;
    }
}
